﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using DDOS.Game_State;
using DDOS.Camera;

namespace DDOS.Objects
{
    class Player : Entity
    {
        private bool isDead;
        private List<Minion> minions;
        private Decimal currency;
        private const Decimal SPEED = 2m;
        int time;
        int inv;
        Rectangle spriteRectangle, sourceRectangle0,sourceRectangle1,sourceRectangle2,sourceRectangle3,sourceRectangle4,sourceRectangle5,sourceRectangle6,sourceRectangle7, sourceRectangle8;
        Random ran = new Random();
        const int CONTROL_DIST = 175;

        public void init_camera(Camera3D cam)
        {
            this.camera = cam;
        }

        public List<Minion> Minions
        {
            get { return minions; }
        }

        private BoundingCircle bounding_circle;
        public BoundingCircle boundingCircle
        {
            get { return bounding_circle; }
            set { bounding_circle = value; bounding_circle.parent = this; }
        }

        public bool dead()
        {
            return isDead;
        }

        public override void InitializeComponents()
        {
            time = 0;
            minions = new List<Minion>();
            minions.Add(new Minion("00"));
            minions.Add(new Minion("01"));
            minions.Add(new Minion("10"));
            minions.Add(new Minion("11"));
            isDead = false;
            this.maxHealth = 10;
            this.health = 10;
            this.power = 0;
            inv = 0;
            this.speed = 2;
            this.sight = 0;
            this.currency = 0;
            this.texture = ImageManager2D.Instance.Animated_Player;
            InitialCoordinates(240, 240);
            boundingObject = new BoundingRectangle((int)coordinates.X, (int)coordinates.Y, (int)coordinates.X + 50, (int)coordinates.Y + 50);
            boundingCircle = new BoundingCircle(coordinates.X,coordinates.Y,(float)this.sight);

            CollisionEngine.Instance.Add(boundingObject);
            //ADD BOUNDING CIRCLE HERE TOO???

            spriteRectangle = new Rectangle((int)coordinates.X, (int)coordinates.Y, 50, 50);
           // sourceRectangle1 = new Rectangle(0, 0, 67, 74);
            sourceRectangle2 = new Rectangle(68, 0, 67, 74);
            sourceRectangle3 = new Rectangle(136, 0, 67, 74);
            sourceRectangle4 = new Rectangle(204, 0, 67, 74);
            sourceRectangle5 = new Rectangle(272, 0, 67, 74);
            sourceRectangle6 = new Rectangle(340, 0, 67, 74);
            sourceRectangle7 = new Rectangle(408, 0, 67, 74);
            sourceRectangle8 = new Rectangle(476, 0, 67, 74);
            this.velocity = new Vector2(0,0);
            blocked = false;
            foreach (Minion m in minions)
            {
                m.init_random(ran);
                m.pass_origin(this.coordinates);
                m.InitialCoordinates((int)this.coordinates.X, (int)this.coordinates.Y);
                m.InitializeComponents();

                CollisionEngine.Instance.Add(m.boundingObject);
                CollisionEngine.Instance.Add(m.boundingCircle);
            }
        }

        public void add_to_score(int i)
        {
            currency += i;
        }

        private void input()
        {
            Vector2 left = InputManager.InputManagerInstance.GetGamePadThumbstickState(InputManager.GamePadThumbstick.left);
            this.velocity = Vector2.Zero;

            if (left != Vector2.Zero)
            {
                if (!blocked)
                {
                    this.velocity.X += left.X * (float)/*SPEED*/speed;
                    this.velocity.Y += left.Y * (float)/*SPEED*/speed;
   
                }
                else
                {
                    this.velocity.X = 0;
                    this.velocity.Y = 0;
                }
            }
            else
            {
                this.velocity = Vector2.Zero;
            }

            if (InputManager.InputManagerInstance.KeyIsDown(Keys.W))
                this.velocity.Y = -(float)speed;
            if (InputManager.InputManagerInstance.KeyIsDown(Keys.S))
                this.velocity.Y = (float)speed;
            if (InputManager.InputManagerInstance.KeyIsDown(Keys.A))
                this.velocity.X = -(float)speed;
            if (InputManager.InputManagerInstance.KeyIsDown(Keys.D))
                this.velocity.X = (float)speed;
        }

        public override void Update(GameTime game_time)
        {
            foreach (Minion m in minions)
            {
                m.pass_origin(this.coordinates + new Vector2(spriteRectangle.Width/2, spriteRectangle.Height/2) +
                    InputManager.InputManagerInstance.GetGamePadThumbstickState(InputManager.GamePadThumbstick.right)*CONTROL_DIST);
                m.Update(game_time);
            }
            input();

            this.coordinates += velocity;

            this.boundingObject.Move(this.velocity.X, this.velocity.Y);
            this.boundingCircle.Move(this.velocity.X, this.velocity.Y);

            this.spriteRectangle.X = (int)this.coordinates.X;
            this.spriteRectangle.Y = (int)this.coordinates.Y;

            time += 1;
            if (inv > 0)
            {
                inv--;
            }
            if (health <= 0)
            {
              //  isDead = true;
                TerminalLayer tlayer = new TerminalLayer();
                TerminalScreen tScreen = new TerminalScreen();
                tScreen.PushLayer(tlayer);
                ScreenManager.Instance.LoadScreen(tScreen);
            }
            
        }

        public override void CollisionCallback(BoundingObject other, BoundingObject self)
        {
            if (other.parent.GetType() == typeof(Wall))
            {
                BoundingBox thisBox = ((BoundingRectangle)self).boundingBox;
                BoundingBox otherBox = ((BoundingRectangle)other).boundingBox;

                Vector2 adjust = new Vector2(0, 0);

                if ((thisBox.Min.X < otherBox.Max.X && thisBox.Min.X > otherBox.Min.X) ^
                    (thisBox.Max.X > otherBox.Min.X && thisBox.Max.X < otherBox.Max.X))
                {
                    adjust.X = -velocity.X;
                }
                if ((thisBox.Min.Y < otherBox.Max.Y && thisBox.Min.Y > otherBox.Min.Y) ^
                    (thisBox.Max.Y > otherBox.Min.Y && thisBox.Max.Y < otherBox.Max.Y))
                {
                    adjust.Y = -velocity.Y;
                }

                this.coordinates += adjust;

                this.boundingObject.Move(adjust.X, adjust.Y);

                this.boundingCircle.Move(adjust.X, adjust.Y);

                this.spriteRectangle.X = (int)this.coordinates.X;
                this.spriteRectangle.Y = (int)this.coordinates.Y;
            }
            else if (other.parent.GetType() == typeof(Enemy0))
            {
                if (other.parent.active() && other == other.parent.boundingObject)
                {
                    if (inv <= 0)
                    {
                        Enemy0 obj = (Enemy0)other.parent;
                        this.health -= obj.get_power();
                        inv = 50;
                    }
                }
            }
            else if (other.parent.GetType() == typeof(Boss1) && other == other.parent.boundingObject)
            {
                if (other.parent.active())
                {
                    if (inv <= 0)
                    {
                        Boss1 obj = (Boss1)other.parent;
                        this.health -= obj.get_power();
                        inv = 50;
                    }
                }
            }
        }

        public override void Draw()
        {

            if (inv > 0)
            {
                ScreenManager.Instance.CurrentSpriteBatch.Draw(this.texture, spriteRectangle, Color.Red);
            }
            else
            {
                /*   if (time <= 15)
                   {
                       ScreenManager.Instance.CurrentSpriteBatch.Draw(texture, spriteRectangle, sourceRectangle0, Color.White);
                   }*/
                if (time <= 15)
                {
                    ScreenManager.Instance.CurrentSpriteBatch.Draw(texture, spriteRectangle, sourceRectangle2, Color.White);
                }
                else if (time <= 30)
                {
                    ScreenManager.Instance.CurrentSpriteBatch.Draw(texture, spriteRectangle, sourceRectangle3, Color.White);
                }
                else if (time <= 45)
                {
                    ScreenManager.Instance.CurrentSpriteBatch.Draw(texture, spriteRectangle, sourceRectangle4, Color.White);
                }
                else if (time <= 60)
                {
                    ScreenManager.Instance.CurrentSpriteBatch.Draw(texture, spriteRectangle, sourceRectangle5, Color.White);
                }
                else if (time <= 75)
                {
                    ScreenManager.Instance.CurrentSpriteBatch.Draw(texture, spriteRectangle, sourceRectangle6, Color.White);
                }
                else if (time <= 90)
                {
                    ScreenManager.Instance.CurrentSpriteBatch.Draw(texture, spriteRectangle, sourceRectangle7, Color.White);

                }
                else if (time <= 105)
                {
                    ScreenManager.Instance.CurrentSpriteBatch.Draw(texture, spriteRectangle, sourceRectangle8, Color.White);
                }
                else
                {
                    ScreenManager.Instance.CurrentSpriteBatch.Draw(texture, spriteRectangle, sourceRectangle2, Color.White);
                    time = 0;
                }
  
            }
            foreach (Minion m in minions)
            {
                m.Draw();
            }

            Vector2 HPOffest = new Vector2(22, 22);
            ScreenManager.Instance.CurrentSpriteBatch.DrawString(FontManager.Instance.Times12, health.ToString(), coordinates+HPOffest, Color.Red);
        }
    }
}
